#include "DefaultParameterManager.h"

DefaultParameterManager *DefaultParameterManager::instance = NULL;

DefaultParameterManager* DefaultParameterManager::getInstance(){
	if(DefaultParameterManager::instance==NULL)
		DefaultParameterManager::instance = new DefaultParameterManager();
	return DefaultParameterManager::instance;
}

DefaultParameterManager::~DefaultParameterManager(){}

void DefaultParameterManager::destroy(){
	if(DefaultParameterManager::instance!=NULL)
		delete DefaultParameterManager::instance;
	DefaultParameterManager::instance = NULL;
}

DefaultParameterManager::DefaultParameterManager(){}

void DefaultParameterManager::fillContainer(MapList* elementMap){
	int resolution;
	try{
		CustomizedMap *mapGeneral = elementMap->getElement("resolucion");
		ValidatorUtils::converToInt(mapGeneral->getAttribute("resolucion").c_str(),&resolution);
		DefaultParameterContainer::getInstance()->setScreenResolution(resolution);
		delete mapGeneral;
	}catch(ElementNotFoundException &e){}
	catch(AttributeNotFoundException &e){}
	catch(InvalidIntegerException &e){
		Logger::get_instance()->log(e);
	}
	catch(InvalidScreenResolutionException &e){
		Logger::get_instance()->log(e);
		Logger::get_instance()->log("Utilizando resolucion por default 800 x 600");
	}
	try{
		std::string colorFondo;
		CustomizedMap *mapGeneral = elementMap->getElement("colorFondoGraf");
		colorFondo = mapGeneral->getAttribute("colorFondoGraf");
		DefaultParameterContainer::getInstance()->setScreenColor(ColorFactory::getInstance()->getColor(colorFondo));
		delete mapGeneral;
	}catch(ElementNotFoundException &e){
	}catch(AttributeNotFoundException &e){
	}catch(InvalidColorException &e){
		Logger::get_instance()->log(e);
	}

	try{
		std::string textureStr;
		CustomizedMap *mapGeneral = elementMap->getElement("textura");
		textureStr = mapGeneral->getAttribute("textura");
		Texture *defaultTexture = TextureContainer::getInstance()->getTextureById(textureStr);
		delete mapGeneral;
	}catch(ElementNotFoundException &e){
	}catch(AttributeNotFoundException &e){
	}catch(TextureNotFoundException &e){
		Logger::get_instance()->log(e);	
	}

	try{
		std::string lineColorStr;
		CustomizedMap *mapGeneral = elementMap->getElement("colorLinea");
		lineColorStr = mapGeneral->getAttribute("colorLinea");
		DefaultParameterContainer::getInstance()->setLineColor(ColorFactory::getInstance()->getColor(lineColorStr));
		delete mapGeneral;
	}catch(ElementNotFoundException &e){
	}catch(AttributeNotFoundException &e){
	}catch(InvalidColorException &e){
		Logger::get_instance()->log(e);
	}

	try{
		std::string figureColorStr;
		CustomizedMap *mapGeneral = elementMap->getElement("colorFondo");
		figureColorStr = mapGeneral->getAttribute("colorFondo");
		Color *figureColor = ColorFactory::getInstance()->getColor(figureColorStr);
		delete mapGeneral;
	}catch(ElementNotFoundException &e){
	}catch(AttributeNotFoundException &e){
	}catch(InvalidColorException &e){
		Logger::get_instance()->log(e);
	}
}

void DefaultParameterManager::manageGeneralTag(std::list<MapList*> elements){
	std::list<MapList*>::iterator it;
	std::string type;

	for(it=elements.begin();it!=elements.end();++it){
		MapList* elementMap = (MapList*)(*it);
		
		std::map<string,CustomizedMap>::iterator elementIt;
		type = elementMap->getType();
		try{
			ObjectType objType = ObjectTypeUtils::getObjectTypeByString(type);
			if(ObjectTypeUtils::isGeneralTag(objType)){
				if(elementMap->isValid())
					fillContainer(elementMap);
				else
					Logger::get_instance()->log("Invalido tag default.Utilizando parametros por defecto del programa");
			}
		}catch(InvalidObjectTypeException &e){	
			//Logger::get_instance()->log(e);
		}
	}
}